package yxy.game.pm2.module.shop;

import org.joda.time.DateTime;
import org.joda.time.LocalDate;

import yxy.cherry.data.DataCenter;

public class Goods {

	private int id;// 商品id
	private int dataId;// 数据id
	private int sell;// 购买次数
	private DateTime updateTime; // 更新时间

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public int getDataId() {
		return dataId;
	}

	public void setDataId(int dataId) {
		this.dataId = dataId;
	}

	public int getSell() {
		return sell;
	}

	public void setSell(int sell) {
		this.sell = sell;
	}

	public DateTime getUpdateTime() {
		return updateTime;
	}

	public void setUpdateTime(DateTime updateTime) {
		this.updateTime = updateTime;
	}

	// func===========================================================================

	public <V> V data(Class<V> clazz) {
		return DataCenter.getData(getDataId(), clazz);
	}

	public boolean sellout(int limit) {
		if (limit <= 0) {
			return false;
		}
		return getSell() >= limit;
	}

	public void reset(DateTime now) {
		setUpdateTime(now);
		setSell(0);
	}

	public boolean update(DateTime now, GoodsLimitType limitType) {
		if (new LocalDate(now).equals(new LocalDate(getUpdateTime()))) {
			// 当日 不更新
			return false;
		}
		switch (limitType) {
		case 不限购:
			break;
		case 日限购:
			reset(now);
			return true;
		case 周限购:
			if (new LocalDate(now).getWeekOfWeekyear() != new LocalDate(getUpdateTime()).getWeekOfWeekyear()) {
				reset(now);
				return true;
			}
			break;
		case 月限购:
			if (new LocalDate(now).getMonthOfYear() != new LocalDate(getUpdateTime()).getMonthOfYear()) {
				reset(now);
				return true;
			}
			break;
		case 需重置:
			break;
		}
		return false;
	}
}
